package com.dmap.admin.controller.backgroundv3;

import com.dmap.admin.constants.VersionConstant;
import com.dmap.admin.controller.backgroundv3.param.AppVersionEditParam;
import com.dmap.admin.controller.backgroundv3.param.AppVersionParam;
import com.dmap.admin.po.AppVersion;
import com.dmap.admin.service.backgroundv3.APPVersionService;
import com.dmap.admin.vo.backgroundv3.AppVersionInfoVo;
import com.dmap.admin.vo.backgroundv3.AppVersionSelectVo;
import com.dmap.base.constants.Constant;
import com.dmap.base.vo.BaseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: zmh
 * @create: 18-8-22
 * @description: app版本controller
 **/
@Api(value = "app版本", description = "background/app版本")
@RestController
@RequestMapping(value = "/background/dmap-knowledge-server/v1/appVersion/")
public class AppVersionBackController {
    @Resource
    private APPVersionService appVersionService;

    @ResponseBody
    @ApiOperation(value = "新增app版本", notes = "新增app版本")
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public BaseVo insertAppVersion(@RequestBody AppVersionParam param) {
        BaseVo result = new BaseVo();
//        if (StringUtils.isBlank(param.getAppNumber()) || StringUtils.isBlank(param.getAndroidVersion()) || StringUtils.isBlank(param.getIosVersion())) {
        if (StringUtils.isBlank(param.getAppNumber())) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        int success = appVersionService.insertAppVersion(param);
        if (success == 0) {
            result.setCode(Constant.RESPONSE_INSERT_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_INSERT_ERROR_MSG);
        }
        return result;
    }

    @ResponseBody
    @ApiOperation(value = "修改app版本", notes = "修改app版本")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public BaseVo updateAppVersion(@RequestBody AppVersionEditParam param) {
        BaseVo result = new BaseVo();
        if (StringUtils.isBlank(param.getAppVersionId()) || !StringUtils.isNumeric(param.getAppVersionId()) || StringUtils.isBlank(param.getAppNumber()) || StringUtils.isBlank(param.getAndroidVersion()) || StringUtils.isBlank(param.getIosVersion())) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        int success = appVersionService.updateAppVersion(param);
        if (success == 0) {
            result.setCode(Constant.RESPONSE_UPDATE_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_UPDATE_ERROR_MSG);
        }
        if (success == 2) {
            result.setCode(VersionConstant.RESPONSE_APP_VERSION_EXIST_ERROR_CODE);
            result.setMsg(VersionConstant.RESPONSE_APP_VERSION_EXIST_ERROR_MSG);
        }
        return result;
    }


    @Transactional
    @ResponseBody
    @ApiOperation(value = "删除app版本", notes = "删除app版本")
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public BaseVo deleteAppVersion(@RequestParam String appVersionIds) {
        BaseVo result = new BaseVo();
        if (StringUtils.isBlank(appVersionIds)) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        int success = appVersionService.deleteAppVersionList(appVersionIds);
        if (success == 0) {
            result.setCode(Constant.RESPONSE_DELETE_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_DELETE_ERROR_MSG);
        }
        if (success == 3) {
            result.setCode(VersionConstant.RESPONSE_APP_VERSION_DELETE_ERROR_CODE);
            result.setMsg(VersionConstant.RESPONSE_APP_VERSION_DELETE_ERROR_MSG);
        }
        return result;
    }

    @ResponseBody
    @ApiOperation(value = "查询app版本", notes = "查询app版本")
    @RequestMapping(value = "/select", method = RequestMethod.GET)
    public BaseVo selectArticle(@RequestParam(required = false) String companyNumber,
                                @RequestParam(required = false) String appNumber,
                                @RequestParam String lang,
                                @RequestParam(required = false) String columnId,
                                @RequestParam(required = false) String key,
                                @RequestParam(required = false, defaultValue = "0") long order,
                                @RequestParam int pageIndex,
                                @RequestParam int pageSize) {
        BaseVo result = new BaseVo();
//        if (lang == null || pageIndex == 0 || pageSize == 0) {
//            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
//            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
//            return result;
//        }
//        if (appNumber != null && StringUtils.isBlank(appNumber)) {
//            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
//            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
//            return result;
//        }
//        if (companyNumber != null && StringUtils.isBlank(companyNumber)) {
//            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
//            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
//            return result;
//        }
//        if (columnId != null && !StringUtils.isNumeric(columnId)) {
//            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
//            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
//            return result;
//        }
//        if (StringUtils.isBlank(lang)) {
//            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
//            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
//            return result;
//        }

        List<AppVersionSelectVo> appVersionSelectVoList = new ArrayList<>();
        List<AppVersion> appVersionList = appVersionService.selectAppVersion();
        for(AppVersion appVersion : appVersionList){
            AppVersionSelectVo appVersionSelectVo = new AppVersionSelectVo(appVersion);
            appVersionSelectVoList.add(appVersionSelectVo);
        }
        return result;
    }

    @ResponseBody
    @ApiOperation(value = "查看app版本信息", notes = "查看app版本信息")
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public BaseVo selectAppVersion(@RequestParam String appVersionId) {
        BaseVo result = new BaseVo();
        if(StringUtils.isBlank(appVersionId) || !StringUtils.isNumeric(appVersionId)){
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        AppVersion appVersion = appVersionService.getAppVersion(Long.parseLong(appVersionId));
        if (appVersion == null) {
            result.setCode(VersionConstant.RESPONSE_APP_VERSION_EXIST_ERROR_CODE);
            result.setMsg(VersionConstant.RESPONSE_APP_VERSION_EXIST_ERROR_MSG);
        }
        AppVersionInfoVo appVersionInfoVo = new AppVersionInfoVo(appVersion);
        result.setData(appVersionInfoVo);
        return result;
    }

}
